import { ProFormOptions } from '@/components/pro-form/type'
import { InventoryModel } from '@/api/model/inventory-model'
import { Ref } from 'vue'
import { optionsType } from '~/types/form'
import useGetOrgMemberList from '@/hooks/useGetOrgMemberList'

type Props = {
  handleType: Ref<'add' | 'edit' | 'check'>
  memberLoading: Ref<boolean>
  memberOptions: Ref<Array<any>>
}

export const useInventoryProForm = ({
  handleType,
  memberLoading,
  memberOptions
}: Props) => {
  const [inventoryForm, resetInventoryForm] = useProRef<
    Partial<InventoryModel>
  >({})

  const { warehouseOptions, warehouseLoading } = useGetWarehouseList()

  const inventoryFormOptions = computed<ProFormOptions>(() => {
    const list = [
      {
        formType: 'input',
        key: 'inventoryCode',
        label: '盘点编码',
        disabled: true
      },
      {
        formType: 'input',
        key: 'inventoryName',
        label: '盘点名称'
      },
      {
        formType: 'inputNumber',
        key: 'inventoryNumber',
        min: 0,
        precision: 0,
        label: '盘点资产总数',
        disabled: true
      },
      {
        formType: 'select',
        key: 'inventoryPersonId',
        label: '盘点人',
        options: memberOptions.value,
        loading: memberLoading.value
      },
      {
        formType: 'select',
        key: 'warehouseIdList',
        label: '盘点仓库',
        options: warehouseOptions.value,
        loading: warehouseLoading.value,
        ruleType: 'array',
        multiple: true,
        disabled: handleType.value === 'edit'
      },
      {
        formType: 'select',
        key: 'inventoryType',
        label: '盘点状态',
        options: useDictData('INVENTORY_TYPE').value,
        disabled: true
      },
      {
        formType: 'input',
        key: 'receivePerson',
        label: '申请人',
        disabled: true
      },
      {
        formType: 'input',
        key: 'receiveBranch',
        label: '申请部门',
        disabled: true
      },
      {
        formType: 'date',
        key: 'receiveDate',
        label: '申请日期',
        disabled: true
      },
      {
        formType: 'textarea',
        key: 'remark',
        label: '备注',
        gridItem: {
          span: 2
        },
        noRule: true
      }
    ] as optionsType
    if (handleType.value === 'add') {
      return useSetOptions(list.filter((item) => !item.disabled))
    } else {
      return useSetOptions(list)
    }
  })

  return {
    inventoryForm,
    inventoryFormOptions,
    resetInventoryForm
  }
}
